home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / bsdunix.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  8KB  |  459 lines

  1. /*
  2.     FILE: unix.c
  3.     
  4.     Routines: This file contains the following routines:
  5.         eihalt()
  6.         kbread()
  7.         clksec()
  8.         tmpfile()
  9.         restore()
  10.         stxrdy()
  11.         disable()
  12.         memstat()
  13.         filedir()
  14.         
  15.     Written by Mikel Matthews, N9DVG
  16.     SYS5 stuff added by Jere Sandidge, K4FUM
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <signal.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/time.h>
  23. #include <ctype.h>
  24. #undef    tolower
  25. #undef    toupper
  26. #include <string.h>
  27. #include "global.h"
  28. #include "cmdparse.h"
  29. #include "iface.h"
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/dir.h>
  33. #include <sys/wait.h>
  34. #include "smtp.h"
  35. #include "unix.h"
  36.  
  37. #define    MAXCMD    1024
  38.  
  39. int asy_attach();
  40. unsigned nasy;
  41. extern struct cmds attab[];
  42.  
  43. fileinit(argv0)
  44. char *argv0;
  45. {
  46.     int el;
  47.     char *ep, *cp, *malloc(), *getenv(), *getcwd();
  48.     char tmp[MAXCMD];
  49.     extern char *startup, *config, *userfile, *hosts, *mailspool;
  50.     extern char *mailqdir, *mailqueue, *routeqdir, *alias, *netexe;
  51. #ifdef  _FINGER
  52.     extern char *fingerpath;
  53. #endif
  54.  
  55.     /* Get the name of the currently executing program */
  56.     if ((cp = malloc((unsigned)(strlen(argv0) + 1))) == NULL)
  57.         perror("malloc");
  58.     else {
  59.         sprintf(cp, "%s", argv0);
  60.         netexe = cp;
  61.     }
  62.  
  63.     /* Try to get home directory name */
  64.     if ((ep = getenv("NETHOME")) == NULLCHAR) {
  65.         if ((ep = getenv("HOME")) == NULLCHAR) {
  66.             ep = ".";
  67.         }
  68.     }
  69.     el = strlen(ep);
  70.     /* Replace each of the file name strings with the complete path */
  71.     if (*startup != '/') {
  72.         if ((cp = malloc((unsigned)(el + strlen(startup) + 2))) == NULL)
  73.             perror("malloc");
  74.         else {
  75.             sprintf(cp, "%s/%s", ep, startup);
  76.             startup = cp;
  77.         }
  78.     }
  79.  
  80.     if (*config != '/') {
  81.         if ((cp = malloc((unsigned)(el + strlen(config) + 2))) == NULL)
  82.             perror("malloc");
  83.         else {
  84.             sprintf(cp, "%s/%s", ep, config);
  85.             config = cp;
  86.         }
  87.     }
  88.  
  89.     if (*userfile != '/') {
  90.         if ((cp = malloc((unsigned)(el + strlen(userfile) + 2))) == NULL)
  91.             perror("malloc");
  92.         else {
  93.             sprintf(cp, "%s/%s", ep, userfile);
  94.             userfile = cp;
  95.         }
  96.     }
  97.  
  98.     if (*hosts != '/') {
  99.         if ((cp = malloc((unsigned)(el + strlen(hosts) + 2))) == NULL)
  100.             perror("malloc");
  101.         else {
  102.             sprintf(cp, "%s/%s", ep, hosts);
  103.             hosts = cp;
  104.         }
  105.     }
  106.  
  107.     if (*alias != '/') {
  108.         if ((cp = malloc((unsigned)(el + strlen(alias) + 2))) == NULL)
  109.             perror("malloc");
  110.         else {
  111.             sprintf(cp, "%s/%s", ep, alias);
  112.             alias = cp;
  113.         }
  114.     }
  115.  
  116. #ifdef      _FINGER
  117.     if (*fingerpath != '/') {
  118.         if ((cp = malloc((unsigned)(el + strlen(fingerpath) + 2))) == NULL)
  119.             perror("malloc");
  120.         else {
  121.             sprintf(cp, "%s/%s", ep, fingerpath);
  122.             fingerpath = cp;
  123.         }
  124.     }
  125. #endif
  126.  
  127.     /* Try to get home directory name */
  128.     if ((ep = getenv("NETSPOOL")) == NULLCHAR)
  129.         ep = "/usr/spool";
  130.     el = strlen(ep);
  131.  
  132.     if (*mailspool != '/') {
  133.         if ((cp = malloc((unsigned)(el + strlen(mailspool) + 2))) == NULL)
  134.             perror("malloc");
  135.         else {
  136.             sprintf(cp, "%s/%s", ep, mailspool);
  137.             mailspool = cp;
  138.         }
  139.     }
  140.  
  141.     if (*mailqdir != '/') {
  142.         if ((cp = malloc((unsigned)(el + strlen(mailqdir) + 2))) == NULL)
  143.             perror("malloc");
  144.         else {
  145.             sprintf(cp, "%s/%s", ep, mailqdir);
  146.             mailqdir = cp;
  147.         }
  148.     }
  149.  
  150.     if (*mailqueue != '/') {
  151.         if ((cp = malloc((unsigned)(el + strlen(mailqueue) + 2))) == NULL)
  152.             perror("malloc");
  153.         else {
  154.             sprintf(cp, "%s/%s", ep, mailqueue);
  155.             mailqueue = cp;
  156.         }
  157.     }
  158.  
  159.     if (*routeqdir != '/') {
  160.         if ((cp = malloc((unsigned)(el + strlen(routeqdir) + 2))) == NULL)
  161.             perror("malloc");
  162.         else {
  163.             sprintf(cp, "%s/%s", ep, routeqdir);
  164.             routeqdir = cp;
  165.         }
  166.     }
  167. }
  168.  
  169. /* action routine for remote reset */
  170. sysreset()
  171. {
  172.     extern char *netexe;
  173.  
  174.     execlp(netexe,netexe,0);
  175.     execlp("net","net",0);
  176.     printf("reset failed: exiting\n");
  177.     exit(1);
  178. }
  179.  
  180. eihalt()
  181. {
  182.     void tnix_scan();
  183.  
  184.     tnix_scan();
  185. }
  186.  
  187. kbread()
  188. {
  189.     int    mask;
  190.     char    c = -1;
  191.     struct timeval timeout;
  192.     timeout.tv_sec = 0;
  193.     timeout.tv_usec = 35;
  194.     mask = 1<<0;
  195.     select(1, &mask, (int *)NULL, (int *)NULL, &timeout);
  196.     if ( mask &= 1<<0 )
  197.     {
  198.         read(fileno(stdin),&c, 1);
  199.     }
  200.     return(c);
  201. }
  202. clksec()
  203. {
  204.     struct timeval time;
  205.     struct timezone zone;
  206.  
  207.     gettimeofday(&time, &zone);
  208.     return(time.tv_sec);
  209. }
  210. FILE *
  211. tmpfile()
  212. {
  213.     FILE *tmp;
  214.     char *mktemp();
  215.     char *ptr = "SMTPXXXXXX";
  216.     char *name;
  217.     name = mktemp(ptr);
  218.     if ( ( tmp = fopen(name, "w") ) == NULL)
  219.     {
  220.         printf("tmpfile: counld not create temp file.\n");
  221.         return(NULL);
  222.     }
  223.     (void) unlink(name);
  224.     return ( tmp );
  225. }
  226.  
  227. /*ARGSUSED*/
  228. restore(state)
  229. char state;
  230. {
  231. }
  232.  
  233. /*ARGSUSED*/
  234. stxrdy(dev)
  235. int16 dev;
  236. {
  237.     return 1;
  238. }
  239.  
  240. disable()
  241. {}
  242.  
  243. memstat()
  244. {
  245.     return(0);
  246. }
  247.  
  248. /* wildcard filename lookup */
  249. filedir (name, times, ret_str)
  250. char *name;
  251. int times;
  252. char *ret_str;
  253. {
  254.     static char     dname[MAXCMD], fname[MAXCMD];
  255.     static DIR *dirp = NULL;
  256.     struct direct *dp;
  257.     struct stat sbuf;
  258.     char    *cp, temp[MAXCMD];
  259.  
  260.     /*
  261.      * Make sure that the NULL is there in case we don't find anything
  262.      */
  263.     ret_str[0] = '\0';
  264.  
  265.     if (times == 0) {
  266.         /* default a null name to *.* */
  267.         if (name == NULL || *name == '\0')
  268.             name = "*.*";
  269.         /* split path into directory and filename */
  270.         if ((cp = rindex(name, '/')) == NULL) {
  271.             strcpy(dname, ".");
  272.             strcpy(fname, name);
  273.         } else {
  274.             strcpy(dname, name);
  275.             dname[cp - name] = '\0';
  276.             strcpy(fname, cp + 1);
  277.             /* root directory */
  278.             if (dname[0] == '\0')
  279.                 strcpy(dname, "/");
  280.             /* trailing '/' */
  281.             if (fname[0] == '\0')
  282.                 strcpy(fname, "*.*");
  283.         }
  284.         /* close directory left over from another call */
  285.         if (dirp != NULL)
  286.             closedir(dirp);
  287.         /* open directory */
  288.         if ((dirp = opendir(dname)) == NULL) {
  289.             printf("Could not open DIR (%s)\n", dname);
  290.             return;
  291.         }
  292.     } else {
  293.         /* for people who don't check return values */
  294.         if (dirp == NULL)
  295.             return;
  296.     }
  297.  
  298.     /* scan directory */
  299.     while ((dp = readdir(dirp)) != NULL) {
  300.         /* test for name match */
  301.         if (wildmat(dp->d_name, fname)) {
  302.             /* test for regular file */
  303.             sprintf(temp, "%s/%s", dname, dp->d_name);
  304.             if (stat(temp, &sbuf) < 0)
  305.                 continue;
  306.             if ((sbuf.st_mode & S_IFMT) != S_IFREG)
  307.                 continue;
  308.             strcpy(ret_str, dp->d_name);
  309.             break;
  310.         }
  311.     }
  312.  
  313.     /* close directory if we hit the end */
  314.     if (dp == NULL) {
  315.         closedir(dirp);
  316.         dirp = NULL;
  317.     }
  318. }
  319.  
  320. /* checks the time then ticks and updates ISS */
  321. static int clkval = 0;
  322. void
  323. check_time()
  324. {
  325.     int32 iss();
  326.  
  327.     if(clkval != clksec()){
  328.         clkval = clksec();
  329.         tick();
  330.         (void)iss();
  331.     }
  332. }
  333.  
  334. getds()
  335. {
  336.     return (0);
  337. }
  338.  
  339. audit()
  340. {
  341. }
  342.  
  343. doshell(argc, argv)
  344. char **argv;
  345. {
  346.     register int pid, pid1, i, (*savi)();
  347.     union wait rc;
  348.     char *cp, str[MAXCMD], *getenv();
  349.     struct sgttyb tt_config;
  350.     extern struct sgttyb savecon;
  351.     
  352.     str[0] = '\0';
  353.     for (i = 1; i < argc; i++) {
  354.         strcat(str, argv[i]);
  355.         strcat(str, " ");
  356.     }
  357.  
  358.     ioctl(0, TIOCGETP, &tt_config);
  359.     ioctl(0, TIOCSETP, &savecon);
  360.  
  361.     if ((cp = getenv("SHELL")) == NULL || *cp != '\0')
  362.         cp = "/bin/sh";
  363.  
  364.     if ((pid = fork()) == 0) {
  365.         if (argc > 1)
  366.             (void)execl("/bin/sh", "sh", "-c", str, 0);
  367.         else
  368.             (void)execl(cp, cp, (char *)0, (char *)0, 0);
  369.         perror("execl");
  370.         exit(1);
  371.     } else if (pid == -1) {
  372.         perror("fork");
  373.         rc.w_status = -1;
  374.     } else {
  375.         savi = signal(SIGINT, SIG_IGN);
  376.         while ((pid1 = wait(&rc)) != pid && pid1 != -1)
  377.             ;    
  378.         signal(SIGINT, savi);
  379.     }
  380.  
  381.     ioctl(0, TIOCSETP, &tt_config);
  382.     return (rc.w_status);
  383. }
  384.  
  385.  
  386. dodir(argc, argv)
  387. char **argv;
  388. {
  389.     register int pid, pid1, i, (*savi)();
  390.     union wait rc;
  391.     char str[MAXCMD];
  392.     struct sgttyb tt_config;
  393.     extern struct sgttyb savecon;
  394.  
  395.     strcpy(str, "ls -l ");
  396.     for (i = 1; i < argc; i++) {
  397.         strcat(str, argv[i]);
  398.         strcat(str, " ");
  399.     }
  400.  
  401.     ioctl(0, TIOCGETP, &tt_config);
  402.     ioctl(0, TIOCSETP, &savecon);
  403.  
  404.     if ((pid = fork()) == 0)
  405.         system(str);
  406.  
  407.     savi = signal(SIGINT, SIG_IGN);
  408.     while ((pid1 = wait(&rc)) != pid && pid1 != -1)
  409.         ;    
  410.     signal(SIGINT, savi);
  411.  
  412.     ioctl(0, TIOCSETP, &tt_config);
  413.     return (rc.w_status);
  414. }
  415.  
  416. int
  417. docd(argc, argv)
  418. int argc;
  419. char **argv;
  420. {
  421.     char tmp[MAXCMD];
  422.     char *getwd();
  423.  
  424.     if (argc > 1) {
  425.         if (chdir(argv[1]) == -1) {
  426.             printf("Can't change directory\n");
  427.             return 1;
  428.         }
  429.     }
  430.     if (getwd(tmp) != NULL)
  431.         printf("%s\n", tmp);
  432.  
  433.     return 0;
  434. }
  435.  
  436. /*
  437.  * These are here to prevent chars that are already lower or upper
  438.  * case from being turned into gibberish.
  439.  */
  440. char
  441. tolower(c)
  442. char c;
  443. {
  444.     if (isupper(c))
  445.         return(c - 'A' + 'a');
  446.     else
  447.         return(c);
  448. }
  449.  
  450. char
  451. toupper(c)
  452. char c;
  453. {
  454.     if (islower(c))
  455.         return(c - 'a' + 'A');
  456.     else
  457.         return(c);
  458. }
  459.